// LabelEx.cpp : implementation file
//

#include "stdafx.h"

#include "StaticZuoCheng.h"
#include "PatternSetterDlg.h"
#include "LabelEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStaticZuoCheng
IMPLEMENT_SERIAL(CStaticZuoCheng,CStatic,1)

CStaticZuoCheng::CStaticZuoCheng()
{
	m_crText=RGB(0,0,0);
	m_crBorder=RGB(0,0,0);
	m_crBG=RGB(255,255,255);

	m_x = 0;
	m_y = 0;
	m_strSizeId = "";
	m_strTitle = "";
	m_strIsTitle = "0";
	m_nUniqueId = 0;

	m_bResizing = FALSE;
	m_bTranparent = FALSE;
	m_bHover = FALSE;
	m_bTracking = FALSE;
	m_bAllowMove = TRUE;
	m_ToolTip = NULL;
	m_tooltext = _T("");
	m_windowName = _T("");
	m_track = NULL;
}

CStaticZuoCheng::~CStaticZuoCheng()
{
	DeleteToolTip();
}

BEGIN_MESSAGE_MAP(CStaticZuoCheng, CStatic)
//{{AFX_MSG_MAP(CText)
	ON_WM_PAINT()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
	ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOVE()
	ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CStaticZuoCheng::OnPaint() 
{	
	CPaintDC dc(this); 
	dc.SetBkMode(TRANSPARENT);
	dc.SelectObject(this->GetFont());
	
	CRect rect;
	CDC MemDC;	
	CPen BorderPen,*pOldPen;
	CBrush BGBrush,*pOldBrush;
	
	this->GetClientRect(&rect);
	MemDC.CreateCompatibleDC(&dc);
	MemDC.SetMapMode(dc.GetMapMode());
	
	BorderPen.CreatePen(PS_SOLID,1,m_crBorder);
	BGBrush.CreateSolidBrush(m_crBG);
	
	pOldPen=dc.SelectObject(&BorderPen);
	pOldBrush=dc.SelectObject(&BGBrush);
	
	//dc.FillRect(rect,&BGBrush);
	dc.Rectangle(rect);

	dc.SelectObject(pOldBrush);
	dc.SelectObject(pOldPen);

	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	lf.lfHeight = 15; 
	wcscpy(lf.lfFaceName, L"Arial"); 

	CFont font1;
	font1.CreateFontIndirect(&lf);
	CFont* font2 = CFont::FromHandle(font1);
	CFont *pOldFont = dc.SelectObject(font2);

	int nTextLeft=0,nTextTop=0;
	CSize size;
	CString strText;
	this->GetWindowText(strText);
	size = dc.GetTextExtent(strText);
	nTextLeft=rect.top+(rect.Width()-size.cx)/2;

	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	nTextTop=rect.top+(rect.Height()-tm.tmHeight)/2;
	if(strText.GetLength()>0)
	{
		dc.TextOut(nTextLeft,nTextTop,strText);
	}
	dc.SelectObject(pOldFont);
	font1.DeleteObject();  


}

void CStaticZuoCheng::SetTextColor(COLORREF crText)
{
	m_crText=crText;
}

void CStaticZuoCheng::SetBorder(BOOL bBorder, COLORREF crBorder)
{
	m_bBorder=bBorder;
	m_crBorder=crBorder;
}

void CStaticZuoCheng::SetBkColor(COLORREF crBkColor)
{
	m_crBG=crBkColor;
	
}

void CStaticZuoCheng::OnLButtonDblClk( UINT nFlags, CPoint point )
{

}

void CStaticZuoCheng::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (!m_bTracking)
	{
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(tme);
		tme.hwndTrack = m_hWnd;
		tme.dwFlags = TME_LEAVE|TME_HOVER;
		tme.dwHoverTime = 1;
		m_bTracking = _TrackMouseEvent(&tme); 
		m_point = point;
	}
	CStatic::OnMouseMove(nFlags,point);
}
LRESULT CStaticZuoCheng::OnMouseHover(WPARAM wparam, LPARAM lparam) 
{
	if (!m_bHover)
	{
		CStatic::Invalidate();
	}
	m_bHover=TRUE;
	::SetActiveWindow(GetParent()->GetParent()->GetSafeHwnd());
	DeleteToolTip();
	SetToolTipText(m_tooltext);
	if (m_ToolTip != NULL)
		if (::IsWindow(m_ToolTip->m_hWnd))
			m_ToolTip->Update();
	return 0;
}

LRESULT CStaticZuoCheng::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
	m_bTracking = FALSE;
	m_bHover=FALSE;
	RedrawWindow();
	return 0;
}

void CStaticZuoCheng::OnLButtonDown( UINT nFlags, CPoint point )
{
	if ((IsMoveable() == TRUE) && (m_bResizing == FALSE)) 
	{
		CPatternSetterDlg *pDlg = ((CPatternSetterDlg*)GetParent()->GetParent());
		if (pDlg == NULL)
				return;

		m_point = point;
		CRect rect;
		BOOL bSuccess = FALSE;
		GetClientRect(rect);
		m_oldRect = rect;
		MapWindowPoints(GetParent()->GetParent(),m_oldRect);

		int nSceneGridWidth,nSceneGridHeight;
		pDlg->m_staticSceneDS1.GetGridWidthAndHeight(nSceneGridWidth , nSceneGridHeight);
		

		rect.right = rect.left + nSceneGridWidth * m_xSpan;
		rect.bottom = rect.top + nSceneGridHeight * m_ySpan;

		m_track = new CRectTracker(&rect, CRectTracker::solidLine | 
								CRectTracker::resizeInside |
								CRectTracker::hatchedBorder);
		if (nFlags & MK_CONTROL) 
		{
// 			GetWindowRect(rect);
// 			GetParent()->ScreenToClient(&rect);
// 			bSuccess = m_track->TrackRubberBand(GetParent(),rect.TopLeft());//Let user resize window
// 			m_track->m_rect.NormalizeRect();
		}
		else 
		{
			bSuccess = m_track->Track(GetParent()->GetParent(), point);
		}
		if (bSuccess)
		{
			rect = LPRECT(m_track->m_rect);

			CPoint ptLeft = pDlg->CalulateNewAreaTopPoint(rect);
			CRect areaRectLeft = pDlg->m_staticSceneDS1.GetAreaRect(ptLeft.x,ptLeft.y,m_xSpan,m_ySpan);

			CPoint ptRight = pDlg->CalulateNewAreaTopPoint(rect,2);
			CRect areaRectRight = pDlg->m_staticSceneDS2.GetAreaRect(ptRight.x,ptRight.y,m_xSpan,m_ySpan);
			
			CString strMianId = ((CPatternSetterDlg*)GetParent()->GetParent())->m_strMianId;
			if (pDlg->m_staticSceneDS1.IsRectInScene(areaRectLeft) && ( !pDlg->IsNewAreaUsed(-2,areaRectLeft,strMianId) ) /* || ptLeft.x >12 || ptLeft.y > 16 */)
			{
				CLabelEx *pNewArea = new CLabelEx();		
				pNewArea->m_strIsTitle = "0";
				pNewArea->m_strSizeId = m_strSizeId;
				pNewArea->m_rect = areaRectLeft;
				pNewArea->m_x = ptLeft.x;
				pNewArea->m_y = ptLeft.y;
				pNewArea->m_xSpan = m_xSpan;
				pNewArea->m_ySpan = m_ySpan;	
				pNewArea->m_strPageFlag = L"1";
				pNewArea->m_strMianId = strMianId;
				/*pNewArea->m_strTitle = strMianId;*/
				vector<CLabelEx*>::iterator iter;
				int nUni=0;
				for (iter = pDlg->m_arrLabels.begin();iter!=pDlg->m_arrLabels.end();iter++)
				{				
					(*iter)->m_nUniqueId = nUni;
					nUni = nUni+1;			
				}
				pNewArea->m_nUniqueId = nUni;
				pDlg->m_arrLabels.push_back(pNewArea);
				
				pNewArea->Create(_T(""), SS_LEFTNOWORDWRAP | SS_NOTIFY | WS_GROUP,areaRectLeft, 
					GetParent()->GetParent(),pNewArea->m_nUniqueId + 1780);
				if (pNewArea->m_strIsTitle == "1" )
					pNewArea->SetBkColor(RGB(228,200,20));
				else
					pNewArea->SetBkColor(RGB(22,200,220));
				
				pNewArea->SetBorder(TRUE,RGB(128,0,200));	

				((CPatternSetterDlg*)GetParent()->GetParent())->ShowSuccessful(3);
				((CPatternSetterDlg*)GetParent()->GetParent())->m_bChanged = TRUE;
			}
			else if ( pDlg->m_staticSceneDS2.IsRectInScene(areaRectRight) && !pDlg->IsNewAreaUsed(-2,areaRectRight,strMianId) )
			{
				/*CString strMianId = ((CPatternSetterDlg*)GetParent()->GetParent())->m_strMianId;*/
				CLabelEx *pNewArea = new CLabelEx();		
				pNewArea->m_strIsTitle = "0";
				pNewArea->m_strSizeId = m_strSizeId;
				pNewArea->m_rect = areaRectRight;
				pNewArea->m_x = ptRight.x;
				pNewArea->m_y = ptRight.y;
				pNewArea->m_xSpan = m_xSpan;
				pNewArea->m_ySpan = m_ySpan;	
				pNewArea->m_strPageFlag = L"2";
				pNewArea->m_strMianId = strMianId;
				/*pNewArea->m_strTitle = strMianId;*/
				vector<CLabelEx*>::iterator iter;
				int nUni=0;
				for (iter = pDlg->m_arrLabels.begin();iter!=pDlg->m_arrLabels.end();iter++)
				{				
					(*iter)->m_nUniqueId = nUni;
					nUni = nUni+1;			
				}
				pNewArea->m_nUniqueId = nUni;
				pDlg->m_arrLabels.push_back(pNewArea);
				
				pNewArea->Create(_T(""), SS_LEFTNOWORDWRAP | SS_NOTIFY | WS_GROUP,areaRectRight, 
					GetParent()->GetParent(),pNewArea->m_nUniqueId + 1780);
				if (pNewArea->m_strIsTitle == _T("1") )
					pNewArea->SetBkColor(RGB(228,200,20));
				else
					pNewArea->SetBkColor(RGB(22,200,220));
				
				pNewArea->SetBorder(TRUE,RGB(128,0,200));	

				((CPatternSetterDlg*)GetParent()->GetParent())->ShowSuccessful(3);
				((CPatternSetterDlg*)GetParent()->GetParent())->m_bChanged = TRUE;
			}
			else
			{
				return;
			}
			//((CPatternSetterDlg*)GetParent()->GetParent())->GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(L"");
		}
		delete m_track;
		m_track = NULL;
		rect = NULL;
		m_bResizing = FALSE;
	}
	CStatic::OnLButtonDown(nFlags,point);
}

BOOL CStaticZuoCheng::PreTranslateMessage(MSG* pMsg) 
{
	if (m_ToolTip != NULL)
		if (::IsWindow(m_ToolTip->m_hWnd))
			m_ToolTip->RelayEvent(pMsg);		
	return CStatic::PreTranslateMessage(pMsg);
}

void CStaticZuoCheng::SetToolTipText(UINT nId, BOOL bActivate)
{
	m_tooltext.LoadString(nId);
	if (m_tooltext.IsEmpty() == FALSE)
		SetToolTipText(m_tooltext, bActivate);

}

void CStaticZuoCheng::SetToolTipText(CString spText, BOOL bActivate)
{
	if (spText.IsEmpty()) return;

	InitToolTip();
	m_tooltext = spText;

	if (m_ToolTip->GetToolCount() == 0)
	{
		CRect rectBtn; 
		GetClientRect(rectBtn);
		m_ToolTip->AddTool(this, m_tooltext, rectBtn, 1);
	}

	m_ToolTip->UpdateTipText(m_tooltext, this, 1);
	m_ToolTip->Activate(bActivate);
}

void CStaticZuoCheng::InitToolTip()
{
	if (m_ToolTip == NULL)
	{
		m_ToolTip = new CToolTipCtrl;
		m_ToolTip->Create(this);
		m_ToolTip->Activate(TRUE);
	}
} 

void CStaticZuoCheng::ActivateTooltip(BOOL bActivate)
{
	if (m_ToolTip->GetToolCount() == 0)
		return;

	m_ToolTip->Activate(bActivate);
} 

void CStaticZuoCheng::DeleteToolTip()
{
	if (m_ToolTip != NULL)
	{
		delete m_ToolTip;
		m_ToolTip = NULL;
	}
}

void CStaticZuoCheng::SetRect( CRect rect )
{
	this->m_rect = rect;
}

CRect CStaticZuoCheng::GetRect( void )
{
	return m_rect;
}

void CStaticZuoCheng::OnMove(int x, int y) 
{
	CStatic::OnMove(x, y);
	
	ShowWindow(SW_HIDE);
	CRect rect;
	GetWindowRect(&rect);
	GetParent()->GetParent()->ScreenToClient(&rect);
	GetParent()->GetParent()->InvalidateRect(&rect);
	ShowWindow(SW_SHOW);
}

void CStaticZuoCheng::Serialize(CArchive& ar) 
{
	if (ar.IsStoring())
	{
		CRect rect;
		TCHAR ch[_MAX_PATH];
		GetWindowRect(&rect);
		GetParent()->GetParent()->ScreenToClient(&rect);
		CString str = _T("");
		if (!m_windowName.IsEmpty())
		{
			str = GetWindowName();
			str += _T("=");
		}
		if (!rect.IsRectEmpty())
		{
			str += LongToStr(ch,rect.left);
			str += _T(",");
			str += LongToStr(ch,rect.top);
			str += _T(",");
			str += LongToStr(ch,rect.right);
			str += _T(",");
			str += LongToStr(ch,rect.bottom);
			str += _T("\r\n");
		}
		ar.WriteString(str);
	}
	else
	{
		CRect rect = NULL;
		CString str = _T("");
		ar.ReadString(str);
		if (str.Find(_T("="),0) > 1)
		{
			SetWindowName(str.Left(str.Find(_T("="),0)));
			str.Delete(0,str.Find(_T("="),0)+1);
		}
		rect.left = _wtol(str.Left(str.Find(_T(","),0)));
		str.Delete(0,str.Find(_T(","),0)+1);
		rect.top = _wtol(str.Left(str.Find(_T(","),0)));
		str.Delete(0,str.Find(_T(","),0)+1);
		rect.right = _wtol(str.Left(str.Find(_T(","),0)));
		str.Delete(0,str.Find(_T(","),0)+1);
		rect.bottom = _wtol(str);
		BOOL b = ::MoveWindow(CStatic::m_hWnd,rect.left,rect.top,rect.Width(),rect.Height(),TRUE);
	}
}

HBRUSH CStaticZuoCheng::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = NULL;
	
	if (m_bTranparent)
	{
		pDC->SetBkMode(TRANSPARENT);
		LOGBRUSH brush;
		brush.lbColor = HOLLOW_BRUSH;
		brush.lbHatch = 0;
		brush.lbStyle = 0;
		hbr = CreateBrushIndirect(&brush);
	}
	else
		hbr = CStatic::OnCtlColor(pDC, pWnd, nCtlColor);
	return hbr;
}

void CStaticZuoCheng::OnLButtonUp( UINT nFlags, CPoint point )
{

	CStatic::OnLButtonUp(nFlags,point);	
}

void CStaticZuoCheng::ReplaceSelf()
{
	MoveWindow(m_rect,TRUE);
}